import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import zutil from './util/util'
import moment from './util/moment'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import BusFactory from 'vue-happy-bus';
import './util/directives.js'

Vue.config.productionTip = false

Vue.use(ElementUI);
Vue.prototype.zutil = zutil;
Vue.prototype.moment = moment;
Vue.prototype.$zbus = BusFactory;

Vue.filter('time', function (value, type) {
  type = parseInt(type, 10);
  let formatString = '';
  switch (type) {
    case 1:
      formatString = 'YYYY-MM-DD HH:mm:ss';
      break;
    case 2:
      formatString = 'YYYY-MM-DD HH:mm';
      break;
    default:
      formatString = 'YYYY-MM-DD';
      break;
  }
  return moment(value).format(formatString);
})
Vue.filter('timef', function (value, formatString) {
  formatString = formatString || 'YYYY-MM-DD HH:mm';
  return moment(value).format(formatString);
})

Vue.filter('formatNum', function (value) {
  return (value+ '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
})

Vue.filter('genderFilter', function (value) {
  if(value && value == 2)
    return '女';
  return '男';
})

Vue.filter('bloodDataFilter', function (value) {
  if(zutil.isEmpty(value)){
    return '-';
  }
  return parseFloat(value).toFixed(1);
})
Vue.filter('zbase2img', function (value) {
  if(zutil.isEmpty(value)){
    return '';
  }
  return `data:image/jpg;base64,${value}`;
})

Vue.filter('birthdayToAge', function (strBirthday) {
  if(!strBirthday)
    return;
  let returnAge = 0,
    strBirthdayArr = strBirthday.split('-'),
    birthYear = strBirthdayArr[0],
    birthMonth = strBirthdayArr[1],
    birthDay = strBirthdayArr[2],
    d = new Date(),
    nowYear = d.getFullYear(),
    nowMonth = d.getMonth() + 1,
    nowDay = d.getDate();
  if (nowYear == birthYear) {
    returnAge = 0; //同年 则为0岁
  } else {
    let ageDiff = nowYear - birthYear; //年之差
    if (ageDiff > 0) {
      if (nowMonth == birthMonth) {
        let dayDiff = nowDay - birthDay; //日之差
        if (dayDiff < 0) {
          returnAge = ageDiff - 1;
        } else {
          returnAge = ageDiff;
        }
      } else {
        let monthDiff = nowMonth - birthMonth; //月之差
        if (monthDiff < 0) {
          returnAge = ageDiff - 1;
        } else {
          returnAge = ageDiff;
        }
      }
    } else {
      returnAge = -1; //返回-1 表示出生日期输入错误 晚于今天
    }
  }
  return returnAge; //返回周岁年龄
})

//价格过滤器{{v.price | priceFormat(2,'￥',true)}}
// arg表示原始参数,num表示小数点位,默认保留两位,symbol表示符号,spilt是否进行逗号分隔【true，false】
Vue.filter('priceFormat', function (arg,num=0,symbol,spilt=false) {
  /**
   * 默认数字: {{number|priceFormat()}}
    保留两位小数: {{number|priceFormat(2)}}
    保留三位小数: {{number|priceFormat(3)}}
    添加￥符号并保留两位: {{number|priceFormat(2,'￥')}}
    添加$符号并保留三位: {{number|priceFormat(3,'$')}}
    添加千分号符号: {{number|priceFormat(0,'',true)}}
    添加千分号符号并保留两位: {{number|priceFormat(2,'',true)}}
    添加￥和千分号符号并保留两位: {{number|priceFormat(2,'￥',true)}}
   **/
  arg=Number(arg).toFixed(num);
  if(spilt==true){
    if (arg.indexOf('.') == -1) {
      arg=arg.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }else{
      var parts = arg.split(".");
      parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
      arg=parts.join(".");
    }
  }
  if(symbol){
    arg=symbol+arg;
  }
  return arg;
})

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#app')
